package com.naughty.userlogin02.controller;

import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.bean.User;
import com.naughty.userlogin02.bean.WxUser;
import com.naughty.userlogin02.dao.WxUserDao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class WxUserController {

    @Autowired
    private WxUserDao wxUserDao; //wxUserDao

    @CrossOrigin
    @RequestMapping("/allwxuser")
    public String getWxUserList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = wxUserDao.getWxUserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<WxUser> wxusers = wxUserDao.getAllWxUser("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",wxusers);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
//        System.out.println("信息="+users_json);
        return users_json;
    }
//    @RequestMapping("/deleteWxUser")
//    public String deleteWxUser(String openid){
//        System.out.println(openid);
//        String i = wxUserDao.deleteWxUser(openid);
//        int k = Integer.valueOf(i).intValue();
//        String str = k > 0?"success":"error";
//        return str;
//    }

    //miniprogram//////
    //添加用户
    @RequestMapping("/addwxuser")
    public String addWxUser(WxUser wxUser) {
        System.out.println(wxUser);
        int i = wxUserDao.addWxUser(wxUser);
        String str = i > 0 ? "success" : "error";
        return str;
    }


    @RequestMapping("/updatewxscore")
    public String updateScore(Integer score,
                              String openid){
        int i = wxUserDao.updateScore(openid, score);
        String str = i >0?"success":"error";
        return str;

    }



}
